"""
数据库引擎模块
"""
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from config.database import SQLALCHEMY_DATABASE_URI

# 创建数据库引擎
engine = create_engine(
    SQLALCHEMY_DATABASE_URI,
    pool_recycle=3600,
    pool_pre_ping=True,
    echo=False  # 设置为True可以查看SQL执行日志
)

# 创建会话工厂
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# 创建Base类，用于创建模型类
Base = declarative_base()

def get_db_session():
    """获取数据库会话"""
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

def init_db():
    """初始化数据库，创建所有表"""
    Base.metadata.create_all(bind=engine) 